# -*- coding: utf-8 -*-
from dm_client import DMClient, DMClientConfig
import click

@click.command()
@click.option('--db', 'db_name', required=True, help='Database name')
@click.option('--table', 'table_name', required=True, help='Table name')
@click.option('--count', is_flag=True, help='Return record count only')
def main(db_name, table_name, count, limit):
    # 初始化客户端
    config = DMClientConfig(server_url="http://127.0.0.1:8080")
    client = DMClient(config)
    
    # 获取数据
    if count:
        data = client.fetch_data(db_name, table_name, limit=limit)
        print("Total records in {}.{}: {}".format(db_name, table_name, len(data)))
    else:
        print("Fetching data from {}.{}".format(db_name, table_name))
        data = client.fetch_data(db_name, table_name, limit=limit)
        
        print("\nFetched Data:")
        for idx, row in enumerate(data, 1):
            print("\nRow {}:".format(idx))
            for key, value in row.items():
                print("  {}: {}".format(key, value))

if __name__ == "__main__":
    main()
